﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBModel;
using DBModel.DAL;

namespace CV_Projekt
{
    public partial class FormSuperAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var user = Session["User"] as SuperAdmin;

            if (user == null)
            {
                Response.Redirect("Default.aspx");
            }

            if (!IsPostBack)
            {
                LabelRemoved.Text = Session["LabelRemove.Text"] as string;
                Session["LabelRemove.Text"] = "";
            }
        }

        protected void ButtonPick_Click(object sender, EventArgs e)
        {
            if (DropDownListRemove.SelectedItem.ToString() == "Radera headhunter")
            {
                List<HeadHunter> allHeadHunters = HeadHunterDB.GetAllHeadHunters();
                if (allHeadHunters.Count == 0)
                {
                    LabelRemoved.Text = "Inga Headhunters hittades.";
                }
                else
                {
                    GridViewRemove.DataSource = allHeadHunters;
                    GridViewRemove.DataBind();
                    LabelRemoved.Text = "";
                }
            }
            else if (DropDownListRemove.SelectedItem.ToString() == "Radera arbetssökande")
            {
                List<Applicant> allApplicants = ApplicantDB.GetAllApplicants();
                if (allApplicants.Count == 0)
                {
                    LabelRemoved.Text = "Inga Arbetssökande hittades.";
                }
                else
                {
                    GridViewRemove.DataSource = allApplicants;
                    GridViewRemove.DataBind();
                    LabelRemoved.Text = "";
                }
            }
        }

        protected void GridViewRemove_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridViewRemove.Rows[index];
            int id = int.Parse(row.Cells[0].Text.Trim());
            if (DropDownListRemove.SelectedItem.ToString() == "Radera headhunter")
            {
                if (HeadHunterDB.DeleteHeadHunterById(id))
                {
                    Session["LabelRemove.Text"] = string.Format("Headhunter med ID '{0}' är bortaget.", id);
                }
            }
            else if (DropDownListRemove.SelectedItem.ToString() == "Radera arbetssökande")
            {
                if (ApplicantDB.DeleteApplicantById(id))
                {
                    Session["LabelRemove.Text"] = string.Format("Jobbsökande med ID '{0}' är bortaget.", id);
                }
            }
            Response.Redirect(@"~/SuperAdmin.aspx");
        }
    }
}
